/**
 * 聚合个数/面积 图片的生成
 */
define([ "dojo/_base/declare", 
         "gistech/widgets/ClusterInfoWindow/InfoWindowWidgetBase",
         "dojo/_base/lang",
         "dojo/on",
         "dojo/query",
         "dojo/topic",
         "dojo/dom-style",
         "dojo/dom-class",
         "dojo/dom-construct",
         "dojo/text!./templates/Template.html",
         "gistech/lib/echarts.common.min","dijit/layout/ContentPane", "gistech/util/ajaxUtil",],//,"gistech/lib/echarts-plain",echarts "gistech/lib/echarts.common.min"
         
    function(declare,BaseWidget,lang,on,query,topic, style,domClass,domConstruct,
    		template,echars2,ContentPane,ajaxUtil) {
        return declare([BaseWidget], {
        	//模板页面
            templateString : template,
            clusterType:'landCount',
            constructor : function() {
            	this.inherited(arguments);
            	//
            	ioc.map.clusterDetailFlag=null; //点击统计信息标记 避免重复查询
            },
            
            //组件初始化方法
            postCreate : function() {
            	this.inherited(arguments);
            	this.addEvents();
            },

            //添加事件
            addEvents:function(){
            	topic.subscribe("initClusterTable", lang.hitch(this, this.createClusterTable));// 点击饼状图时 创建右侧表格
            	topic.subscribe("clusterDetailInfo", lang.hitch(this, this.clusterDetailData));//表格中区域地块类型详细信息
            },
            
            /**
             * 根据参数加载饼状图
             */
            initChartPie:function(dimensionName,czldCount,gyjcydCount,sjjcCount,sjjtjcCount,sjjchCount,gysbydCount){
            	var result=[];
            	var color=[];  
            	var legends =[];
//            	:['red', 'green','yellow','blueviolet'] 
//            	if(czldCount!=undefined && czldCount != ''){
//            		result.push({value:czldCount,name:dimensionName+'村自留地:'+czldCount});
//            		color.push('red');
//            		legends.push('村自留地');
//            	}
//            	if(gyjcydCount!=undefined && gyjcydCount != ''){
//            		result.push({value:gyjcydCount,name:dimensionName+'国有旧厂:'+gyjcydCount});
//            		color.push('green');
//            		legends.push('国有旧厂');
//            	}
//            	if(sjjcCount!=undefined && sjjcCount != ''){
//            		result.push({value:sjjcCount,name:dimensionName+'三旧旧村:'+sjjcCount});
//            		color.push('blue');
//            		legends.push('三旧旧村');
//            	}
//            	if(sjjtjcCount!=undefined && sjjtjcCount != ''){
//            		result.push({value:sjjtjcCount,name:dimensionName+'集体旧厂:'+sjjtjcCount});
//            		color.push('blueviolet');
//            		legends.push('集体旧厂');
//            	}
//            	
            	
//            	if(czldCount!=undefined && czldCount != ''){
//            		result.push({value:czldCount,name:'村自留地:'+czldCount});
//            	}
//            	if(gyjcydCount!=undefined && gyjcydCount != ''){
//            		result.push({value:gyjcydCount,name:'国有旧厂:'+gyjcydCount});
//            	}
//            	if(sjjcCount!=undefined && sjjcCount != ''){
//            		result.push({value:sjjcCount,name:'三旧旧村:'+sjjcCount});
//            	}
//            	if(sjjtjcCount!=undefined && sjjtjcCount != ''){
//            		result.push({value:sjjtjcCount,name:'集体旧厂:'+sjjtjcCount});
//            	}
            	
            	if(czldCount!=undefined && czldCount != ''&& czldCount != 0){
            		color.push('#5DA5B8');
            		result.push({value:czldCount,name:'村自留地'});
            	}
            	if(gyjcydCount!=undefined && gyjcydCount != ''&& gyjcydCount != 0){
            		color.push('#FDB961');
            		result.push({value:gyjcydCount,name:'国有用地的旧厂'});
            	}
            	if(sjjcCount!=undefined && sjjcCount != '' && sjjcCount != 0){
            		color.push('#F36D6F');
            		result.push({value:sjjcCount,name:'三旧旧村'});
            	}
            	if(sjjtjcCount!=undefined && sjjtjcCount != ''&& sjjtjcCount != 0){
            		color.push('#F6A26D');
            		result.push({value:sjjtjcCount,name:'集体用地的旧厂'});
            	}
            	if(sjjchCount!=undefined && sjjchCount != ''&& sjjchCount != 0){
            		color.push('#9E81B2');
            		result.push({value:sjjchCount,name:'三旧旧城'});
            	}
            	if(gysbydCount!=undefined && gysbydCount != ''&& gysbydCount != 0){
            		color.push('#75C8B1');
            		result.push({value:gysbydCount,name:'国有储备用地'});
            	}
            	
            	var option = {
            			 tooltip: {
 	    			        trigger: 'item',
 	    			        formatter: "{b}: {c} "
 	    			    },
 	    			    
 	    			   //中间的显示
 	    			   title: {
 	    			        text: dimensionName,
 	    			       // subtext: '2000000.00',
 	    			       fontSize:19, //无效??
 	    			        x: 'center',
 	    			        y: 'center'
 	    			   },
            			 calculable : true,
            			    toolbox: {
            			        show : true,
            			        feature : {
            			            mark : {show: true},
            			            dataView : {show: false, readOnly: false},
            			            magicType : {
            			                show: true, 
            			                type: ['pie', 'funnel'],
            			                option: {
            			                    funnel: {
            			                        x: '25%',
            			                        width: '50%',
            			                        funnelAlign: 'center',
            			                        max: 1548
            			                    }
            			                }
            			            },
            			            restore : {show: false},
            			            saveAsImage : {show: false}
            			        }
            			    },
            			    series : [
            			        {
            			            name:dimensionName,
            			            type:'pie',
            			            radius : ['30%', '70%'],
            			            itemStyle : {
            			                normal : {
            			                    label : {
            			                        show : false
            			                    },
            			                    labelLine : {
            			                        show : false
            			                    }
            			                },
            			                emphasis : {
            			                    label : {
            			                        show : false,
            			                        position : 'center',
            			                        textStyle : {
            			                            fontSize : '8',
            			                            fontWeight : 'bold'//'normal'
            			                        }
            			                    }
            			                }
            			            },
            			            legend: {
            			                orient: 'vertical',
            			                left: 'center',
            			                data: legends
            			            },
            			            data: result,
            			            color:color
            			        }
            			    ]
            	};
            	

            	var chart = echars2.init(this.stat2);
            	chart.setOption(option);
            	  
            	chart.on('click', function (params) {
            		
            	var  cityDistName = params.seriesName;
            	   ioc.map.clusterClickName = params.seriesName;
            	   ioc.map.clusterDetailFlag=null;
            	   topic.publish('initClusterTable',cityDistName)
            	});
            },
            
            //详细信息
            clusterDetailData:function(args){
            	
            	if(args.clusterlandType == undefined){
            		return;
            	}
            	 
            	  var landName = args.clusterlandType;
            	  if(landName == '国有用地的旧厂'){
            		  landName = 2;
            	  }else if(landName == '三旧旧村'){
            		  landName = 7; 
            	  }else if(landName == '集体用地的旧厂'){
            		  landName = 9;
            	  }else if(landName == '村自留地'){
            		  landName = 10;
            	  }else if(landName == '三旧旧城'){
            		  landName = 3;
            	  }else if(landName == '国有储备用地'){
            		  landName = 4;
            	  }
            	  var parameter= "dimension="+ioc.map.ClusterDimension;
            	  parameter += "&landType="+landName;
            	  parameter += "&areaName="+ioc.map.clusterClickName;
            	  
            	  //
            	  if(ioc.map.clusterDetailFlag==null){
            		  ioc.map.clusterDetailFlag = parameter;
            	  }else if(ioc.map.clusterDetailFlag == parameter){
            		  return;
            	  }else{
            		  ioc.map.clusterDetailFlag = parameter;
            	  }
//            	  console.log('00')
            	  
	       			 //后台处理数据
	       	      	ajaxUtil.request("../client/service/clusterController/clusterDetailData",
	       	      		parameter,lang.hitch(this,function(result){
	       	      		 //debugger;
	       	    			 var Store = eval(result.result);
	       	    			 if(Store!=null && Store.length>0){
		       	      			var tableParam={
		       	    					"tableData":Store,
		       	    					"columnsData":this._tableDetailColumnsData(landName),
		       	    					"sortName":'CSMC',
	//	       	    					"sortOrder":'asc'
		       	    				};
		       	    				var bootstrapTableParamSetting={};
		       	    				var tableTitle="地块详细信息列表";
		       	    				this._createTable(tableParam,bootstrapTableParamSetting,tableTitle);
	       	    			 }
	       	    	    })
	       	    	 )
            },
            
            _createTable:function(tableParam,bootstrapTableParamSetting,tableTitle){
            	require(["gistech/widgets/QueryTableList/QueryTableList"],lang.hitch(this,function(QueryTableList){
            		//debugger
    				var queryTableList = new QueryTableList(tableParam);
            		queryTableList.$table.bootstrapTable('refreshOptions',bootstrapTableParamSetting);
    				queryTableList.startup();
    				if($(Global.widgets.right.domNode).children(".queryList").length!=0){
    					$(Global.widgets.right.domNode).children(".queryList").remove();
    				}
    				//为table添加标题信息--定制化
    				$('.fixed-table-toolbar',queryTableList.domNode).css({"height":"42px","background-color":"#f5f5f5","margin-bottom":"5px","padding-top":"10px","border-radius": "4px","margin-top": "6px"});
    				var headHTML='<span style="font-size: 16px;padding-left: 10px;">'+tableTitle+'</span>';
    				domConstruct.place(headHTML, $('.fixed-table-toolbar',queryTableList.domNode)[0], "first");
    				$('.fixed-table-toolbar .columns',queryTableList.domNode).css({"margin-top":"-5px","margin-right":"10px"});
    				$('.fixed-table-toolbar .search',queryTableList.domNode).css({"margin-top":"-28px","margin-left": "158px"});
    				var panel = new ContentPane();
    				$(panel.domNode).wrapInner(queryTableList.domNode);
    				if($('.centerPanel>.dijitContentPane>:nth-child(2)',Global.widgets.right.domNode).length == 1){
    					$('.centerPanel>.dijitContentPane>:nth-child(2)',Global.widgets.right.domNode).remove();
    				}
    				Global.widgets.right.addTab(panel);
    				//当非管理员时不能使用导出Excel
    				if(!ioc.map.userInfoData.isManager){
    					document.getElementsByClassName("btn btn-default dropdown-toggle")[0].style.display='none';
    					document.getElementsByClassName("btn btn-default dropdown-toggle")[1].style.display='none';
    					$('.fixed-table-toolbar .search',queryTableList.domNode).css({"margin-top":"-28px","margin-left": "155px"});
    				}
    				
    			}));
            },
            
    		//地块列数据
    		_bufferBlockColumsData:function(){
            	var arr=[
    			 {field:'CODE',title:'地块编号',width:30,align:'center'},
                 {field:'CSMC',title:'城市名称',width:40,align:'center'},
                 {field:'FQMC',title:'分区名称', width:30,align:'center'},
                 {field:'GYMJ',title:'用地面积',width:40,align:'center'},
                 {field:'JCFNF',title:'旧厂房年份',width:30,align:'center',visible:false},
                 {field:'GZLX',title:'改造类型', width:30,align:'center',visible:false},
                 {field:'NGZYT',title:'拟改造用途',width:30,align:'center',visible:false},
                 {field:'JHGZNF',title:'计划改造年份',width:40,align:'center',visible:false},
                 {field:'QSDWMC',title:'权属单位名称', width:30,align:'center',visible:false}
           		];
      			return arr;
    		},
    		
    		//地块数据对象
    		_bufferBlockDataObj :function(attr){
    			var obj={};
    			obj['CODE']=attr.CODE;
    			obj['CSMC']=attr.CSMC;
    			obj['FQMC']=attr.FQMC;
    			obj['GYMJ']=attr.GYMJ;
    			obj['JCFNF']=attr.JCFNF;
    			obj['GZLX']=attr.GZLX;
    			obj['NGZYT']=attr.NGZYT;
    			obj['JHGZNF']=attr.JHGZNF;
    			obj['QSDWMC']=attr.QSDWMC;
    			return obj;
    		},
          
            //进行表格数据的填充
            createClusterTable:function(cityDistName){
            	//debugger
            	//将右侧组件展开
    			if(!Global.widgets.right.hasExpand){
    				Global.widgets.right.expandLeft();
    			}
    			Global.widgets.right.removeAllTab();
            	//debugger
    			require(["gistech/widgets/QueryTableList/QueryTableList"],lang.hitch(this,function(QueryTableList){
    				//debugger
    				var queryTableList = new QueryTableList({
    					"tableData":this._tableData(cityDistName),
    					"columnsData":this._tableColumnsData(cityDistName),
    					"sortName":null,
    					"sortOrder":null
    				});
    				queryTableList.startup();
    				queryTableList.$table.bootstrapTable('refreshOptions',{detailView:false,search:false});
    				if($(Global.widgets.right.domNode).children(".queryList").length!=0){
    					$(Global.widgets.right.domNode).children(".queryList").remove();
    				}
    				$('.fixed-table-toolbar',queryTableList.domNode).css({"height":"42px","background-color":"#f5f5f5","margin-bottom":"5px","padding-top":"10px","border-radius": "4px","margin-top": "6px"});
    				var headHTML='<span style="font-size: 16px;padding-left: 10px;">聚合统计信息列表</span>';
    				domConstruct.place(headHTML, $('.fixed-table-toolbar',queryTableList.domNode)[0], "first");
    				$('.fixed-table-toolbar .columns',queryTableList.domNode).css({"margin-top":"-5px","margin-right":"10px"});
    				$('.fixed-table-container .pagination-detail',queryTableList.domNode).css("display","none");
    				var panel = new ContentPane();
    				$(panel.domNode).wrapInner(queryTableList.domNode);
    				Global.widgets.right.addTab(panel);
    			}));
            },
            
            _tableColumnsData:function(cityDistName){
            	var arr=[
 
        				 {field:'name',title:'('+cityDistName+')土地类型名称',width:30,align:'center'},
                         {field:'count',title:'宗数(块)',width:40,align:'center'},
                         {field:'area',title:'面积(/公顷)', width:30,align:'center'}
        				 ]
//            	if(this.clusterType=='landArea'){
//            		arr=[
//        				 {field:'name',title:'('+cityDistName+')土地类型名称',width:30,align:'center'},
//                         {field:'count',title:'面积(平米)',width:60,align:'center'}
//        				 ]
//            	}
      			return arr;
    		},
    		

    		
    		_tableData:function(cityDistName){
    			var arr=[];
    			var arr = this.dealClusterData(cityDistName);
//    			var test = {name:cityDistName,count:'200' };
//    			arr.push(test)
    			return arr;
    		},
    		
    		dealClusterData:function(cityDistName){
    			var result =[];
    			var feaset ; 
      		  if(ioc.map.ClusterDimension == 'city'){
      			feaset = ioc.map.shifeatures ;
    		  }else if(ioc.map.ClusterDimension == 'distinct'){
    			  feaset = ioc.map.qufeatures ;
    		  }
    			for(var i=0;i<feaset.length;i++){
    				var fs = feaset[i];
    	        	var tPoint = fs.point;
    	        	var name = fs.name ; 
    	        	var clusdata = ioc.map.clusterData;
    	     		 if(clusdata.length>0){
    	     			 for(var j=0;j<clusdata.length;j++){
    	     				 var data = clusdata[j];
    	     				 var dimensionName = "";
    	     				 if(  ioc.map.ClusterDimension == 'distinct'){
    	     					dimensionName = data.district
    	     				 }else if(  ioc.map.ClusterDimension == 'city'){
    	     					dimensionName = data.city
    	     				 }else if(flag==3 || ioc.map.ClusterDimension == 'province' ){ 
    	     					dimensionName = ""; //TODO 省
    	     				 }
    	     				  
    	     				 if(cityDistName == dimensionName ){
    	     					 var isCity = false;
    	     					   if(ioc.map.ClusterDimension=='city' && data.district==undefined){
    	     							 isCity = true ; 
    	     					   }
    	     					   if( ioc.map.ClusterDimension=='city' && !isCity){break;};
    	     					   var result = this.toObj(dimensionName,data.czldCount,data.gyjcydCount,data.sjjcCount,data.sjjtjcCount,data.sjjchCount,data.gysbydCount,data.czldArea,data.gyjcydArea,data.sjjcArea,data.sjjtjcArea,data.sjjchArea,data.gysbydArea);
//    	     					   if(this.clusterType=='landArea'){
//    	     						  result = this.toObj(dimensionName,data.czldArea,data.gyjcydArea,data.sjjcArea,data.sjjtjcArea,data.sjjchArea,data.gysbydArea);
//    	     					   }
    	     					   return result;
    	     				 }
    	     			 }
    	     		 }
    			}
    		},
    		toObj: function(dimensionName,czldCount,gyjcydCount,sjjcCount,sjjtjcCount,sjjchCount,gysbydCount,czldArea,gyjcydArea,sjjcArea,sjjtjcArea,sjjchArea,gysbydArea){
                	var result=[];
                	 
                	if(czldCount!=undefined && czldCount != '' && czldCount != 0){
                		result.push({name:'村自留地',count:czldCount,area:czldArea});
                	}
                	if(gyjcydCount!=undefined && gyjcydCount != '' && gyjcydCount != 0){
                		result.push({name:'国有用地的旧厂',count:gyjcydCount,area:gyjcydArea});
                	}
                	if(sjjcCount!=undefined && sjjcCount != '' && sjjcCount != 0){
                		result.push({name:'三旧旧村',count:sjjcCount,area:sjjcArea});
                	}
                	if(sjjtjcCount!=undefined && sjjtjcCount != '' && sjjtjcCount != 0){
                		result.push({name:'集体用地的旧厂',count:sjjtjcCount,area:sjjtjcArea});
                	}
                	if(sjjchCount!=undefined && sjjchCount != '' && sjjchCount != 0){
                		result.push({name:'三旧旧城',count:sjjchCount,area:sjjchArea});
                	}
                	if(gysbydCount!=undefined && gysbydCount != '' && gysbydCount != 0){
                		result.push({name:'国有储备用地',count:gysbydCount,area:gysbydArea});
                	}
                	return result;
    		},
    		
            startup:function(dimensionName,czldCount,gyjcydCount,sjjcCount,sjjtjcCount,sjjchCount,gysbydCount){
            	this.inherited(arguments);
//            	this.initChartLine();
            	this.initChartPie(dimensionName,czldCount,gyjcydCount,sjjcCount,sjjtjcCount,sjjchCount,gysbydCount);
            },
            
            
    		//详细列表中的类
            _tableDetailColumnsData:function(landType){
            	
            	var arr = [];
            	
            	//国有旧厂
            	if(landType!=10){
            		arr = [
            		    {field:'OBJECTID',title:'OBJECTID',width:40,visible:false},
       					{field:'CODE',title:'要素代码',width:40,visible:false},
       					{field:'FQMC',title:'分区名称',width:40,visible:false},
       					{field:'CSMC',title:'城市名称', width:40,visible:false},
       					{field:'ZJMC',title:'镇街名称', width:40},
       					{field:'BZSJ',title:'编制时间', width:40 ,visible:false},
       					{field:'XMDKBH',title:'图斑编号', width:40 ,visible:false},
       					{field:'GZLX',title:'改造类型', width:40  ,visible:false},
       					{field:'ZLDWMC',title:'座楼单位名称', width:60,visible:false},
       					{field:'QSDWMC',title:'权属单位名称', width:80},
       					{field:'SYQSX',title:'所有权属性', width:60,visible:false},
       					{field:'TDYT',title:'现土地用途', width:60,visible:false},
       					{field:'GHYT',title:'规划用途', width:60,visible:false},
       					{field:'NGZYT',title:'拟改造用途', width:60,visible:false},
       					{field:'SFBZGH',title:'是否规划', width:40,visible:false},
       					{field:'YDMJ',title:'用地面积', width:40},
       					{field:'GYMJ',title:'其中国有面积', width:60,visible:false},
       					{field:'JTMJ',title:'其中集体面积', width:60,visible:false},
       					{field:'NYDMJ',title:'其中农用地面积', width:60,visible:false},
       					{field:'HFYDMJ',title:'其中合法用地面积', width:60,visible:false},
       					{field:'JHGZNF',title:'计划改造年份', width:60,visible:false},
       					{field:'BZ',title:'备注', width:30,visible:false},
       					{field:'BZ2',title:'标注', width:30,visible:false},
       					{field:'JCFNF',title:'旧村庄年份', width:30,visible:false}, 
                   	    ]
            	}else if(landType==10){
            		arr=[
            		    {field:'OBJECTID',title:'OBJECTID',width:40,visible:false},
						{field:'CODE',title:'要素代码', width:40,visible:false},
						{field:'FQMC',title:'分区名称', width:40,visible:false},
						{field:'CSMC',title:'城市名称', width:40,visible:false},
						{field:'BZSJ', title:'编制时间', width:40,visible:false},
						{field:'SJMC',title:'市界名称', width:40,visible:false},
						{field:'QJMC',title:'行政区名称', width:40,visible:false},
						{field:'ZJMC',title:'镇街名称', width:40},
						{field:'CMC',title:'村（社区）名称', width:40},
						{field:'SYQXZ',title:'所有权性质', width:40,visible:false},
						{field:'QSDW',title:'权属单位', width:60 },
						{field:'XZYT',title:'现状用途', width:40,visible:false},
						{field:'DZ',title:'地址', width:40,visible:false},
						{field:'MJ',title:'面积', width:40 },
						{field:'WH',title:'选址文号', width:40,visible:false},
						{field:'PWH',title:'土地发证文号', width:40,visible:false},
						{field:'BZ',title:'备注', width:40,visible:false},
						{field:'BZ2',title:'标注', width:40,visible:false},
            		     ]
            	}
     
      			return arr;
    		},
    		
    		
            
            destroy:function(){
            	this.inherited(arguments);
            }
        });
    });
